package com.njupt.user.webapp.action;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.njupt.system.model.UserAuthority;
import com.njupt.service.GenericPaginationManager;
import com.njupt.service.UniversalManager;
import com.njupt.blog.Constants;
import com.njupt.auth.service.UserDetailsService;
import com.njupt.auth.model.UserDetails;

@ParentPackage(value = "user")
@Namespace(value = "")
@Results({ 
	@Result(name = "index", location = "/index.jsp"),
	@Result(name = "login", location = "/login.jsp"),
	@Result(name="redirectIndex",location="/login.jsp")
})
public class LoginAction
{
	private String username;
	private String password;
	private UserDetailsService userDetailsService;
	private GenericPaginationManager<UserAuthority, Long> userAuthorityManager;
	private UniversalManager manager;

	public String execute() throws UnsupportedEncodingException
	{
		UserDetails u = userDetailsService.loadUserByUsername(username);
		if (u != null && u.getPassword().equals(password) && u.isEnabled())
		{
			ServletActionContext.getRequest().getSession()
					.setAttribute(Constants.SECURITY_CONTEXT_KEY, u);
			UserAuthority userAuthority = manager.findUniqueBy(UserAuthority.class, "username", username);
			ServletActionContext.getRequest().getSession().setAttribute(Constants.SECURITY_CONTEXT_ROLE, userAuthority.getAuthority());
			ServletActionContext.getRequest().getSession()
					.setAttribute("currentUsername", username);
			String ctx = ServletActionContext.getRequest().getContextPath();
			Cookie usernameCookie = new Cookie("username_" + ctx,
					URLEncoder.encode(username, "UTF-8"));
			Cookie passwordCookie = new Cookie("password_" + ctx,
					URLEncoder.encode(password, "UTF-8"));

			usernameCookie.setPath(ctx);
			passwordCookie.setPath(ctx);

			usernameCookie.setMaxAge(604800);
			passwordCookie.setMaxAge(604800);// 记住周期为一周

			ServletActionContext.getResponse().addCookie(usernameCookie);
			ServletActionContext.getResponse().addCookie(passwordCookie);
			return "index";
		}
		ServletActionContext.getRequest().setAttribute("msg", "用户或密码错误");
		return "login";
	}
	
	public String logout(){
		HttpSession session = ServletActionContext.getRequest().getSession(false);
		if ( session != null) {
		    session.invalidate();
		}
		ServletActionContext.getRequest().getSession(true);
		Cookie usernameCookie = new Cookie("username", null);
		Cookie passwordCookie = new Cookie("password", null);
		
		usernameCookie.setMaxAge(0);
		passwordCookie.setMaxAge(0);
		
		ServletActionContext.getResponse().addCookie(usernameCookie);
		ServletActionContext.getResponse().addCookie(passwordCookie);
		return "redirectIndex";
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public GenericPaginationManager<UserAuthority, Long> getUserAuthorityManager()
	{
		return userAuthorityManager;
	}

	public void setUserAuthorityManager(
			GenericPaginationManager<UserAuthority, Long> userAuthorityManager)
	{
		this.userAuthorityManager = userAuthorityManager;
	}

	public UserDetailsService getUserDetailsService()
	{
		return userDetailsService;
	}

	public void setUserDetailsService(UserDetailsService userDetailsService)
	{
		this.userDetailsService = userDetailsService;
	}

	public UniversalManager getManager()
	{
		return manager;
	}

	public void setManager(UniversalManager manager)
	{
		this.manager = manager;
	}
}
